package com.meihua.baoleyi.fragment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.meihua.baoleyi.R;
import com.meihua.baoleyi.adapters.MainRVAdapter;
import com.meihua.baoleyi.base.BaseRecyclerViewBannerFragment;
import com.meihua.baoleyi.bean.GoodsBean;
import com.meihua.baoleyi.bean.ShopCart;
import com.meihua.baoleyi.common.APIServer;
import com.meihua.baoleyi.common.MyApp;
import com.meihua.baoleyi.net.NetHelper;
import com.meihua.baoleyi.newcityselector.SelectCityActivity;
import com.meihua.baoleyi.ui.CommonWebActivity;
import com.meihua.baoleyi.ui.SearchActivity;
import com.meihua.baoleyi.view.IRecyclerView;
import com.meihua.baoleyi.view.LoadMoreFooterView;

import java.util.ArrayList;
import java.util.TreeMap;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by liming on 2017/10/16
 * 首页界面
 */

public class MainFragment extends BaseRecyclerViewBannerFragment implements MainRVAdapter.OnItemClickListener {

    @Bind(R.id.main_et_search)
    RelativeLayout mainEtSearch;
    @Bind(R.id.rl_main)
    RelativeLayout rl_main;
    @Bind(R.id.rl_goods)
    RelativeLayout rl_goods;
    @Bind(R.id.main_iv_cart)
    ImageView mainIvCart;
    @Bind(R.id.main_rl_city)
    RelativeLayout rl_city;
    @Bind(R.id.main_iRecyclerView)
    IRecyclerView mainIRecyclerView;
    @Bind(R.id.main_tv_address)
    TextView tvAddress;
    @Bind(R.id.iv_red_point)
    ImageView ivRedPoint;
    private ArrayList<GoodsBean.DataBean> mData;
    private MainRVAdapter rvAdapter;
    private String url;
    Context mcontext;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mcontext = activity;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_fragment, container, false);
        ButterKnife.bind(this, view);
        if (null != getArguments()) {
            url = getArguments().getString("url");
        }
        if (url.equals(APIServer.HOME_GETSHOPLIST)) {
            rl_main.setVisibility(View.VISIBLE);
            rl_goods.setVisibility(View.GONE);
        } else {
            rl_main.setVisibility(View.GONE);
            rl_goods.setVisibility(View.VISIBLE);
        }
        initRecyclerView(mainIRecyclerView, new GridLayoutManager(getActivity(), 2), true, url.equals(APIServer.HOME_GETSHOPLIST));
        isShoppingCatEmpty();
        return view;
    }

    @Override
    protected RecyclerView.Adapter getAdapter() {
        mData = new ArrayList<>();
        rvAdapter = new MainRVAdapter(mcontext, mData);
        rvAdapter.setOnItemClickListener(this);
        return rvAdapter;
    }

    @Override
    protected void getServiceData(final int page) {
        if (page == 1) setBannerView(APIServer.BANNERURL, url.equals(APIServer.HOME_GETSHOPLIST));
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("page", page + "");
                put("user_id", MyApp.getInstance().getUser_id());
            }
        };
        NetHelper.get(url, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                GoodsBean bean = NetHelper.getBeanForJson(GoodsBean.class, s);
                if (bean == null) return;
                if (null != bean.getData()) {
                    if (bean.getData().size() != 0) {
                        if (page == 1) {
                            /*刷新*/
                            if (mainIRecyclerView != null) {
                                mainIRecyclerView.setRefreshing(false);
                            }
                            mData.clear();
                        }
                        mData.addAll(bean.getData());
                        rvAdapter.notifyDataSetChanged();
                    } else {
                        mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                    }
                } else {
                    mLoadMoreFooterView.setStatus(LoadMoreFooterView.Status.THE_END);
                }
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }

    @OnClick({R.id.main_rl_city, R.id.main_iv_cart, R.id.main_et_search})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.main_rl_city:
                /*城市定位*/
                Intent intent1 = new Intent(mcontext, SelectCityActivity.class);
                startActivityForResult(intent1, 2);
                break;
            case R.id.main_iv_cart:
                Intent intent = new Intent(getActivity(), CommonWebActivity.class);
                intent.putExtra("url", APIServer.SHOPCAT + MyApp.getInstance().getToken() + "/" + MyApp.getInstance().getUser_id());
                startActivity(intent);
                break;
            case R.id.main_et_search:
                 /*搜索中心界面*/
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
        }
    }

    /*跳转到webview界面*/
    @Override
    public void onItemClick(GoodsBean.DataBean bean) {
        Intent intent = new Intent(getActivity(), CommonWebActivity.class);
        intent.putExtra("url", APIServer.GOODSDETAIL + MyApp.getInstance().getToken() + "/" + MyApp.getInstance().getUser_id() + "/" + bean.getGoods_id());
        startActivity(intent);
    }

    /*上一界面返回的数据*/
    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 2 && data != null) {
            tvAddress.setText(data.getStringExtra("city"));
        }
    }

    /*实例化*/
    public static MainFragment newInstance(String url) {
        MainFragment mInstance = new MainFragment();
        Bundle args = new Bundle();
        args.putString("url", url);
        mInstance.setArguments(args);
        return mInstance;
    }

    public void isShoppingCatEmpty() {
        TreeMap<String, String> treeMap = new TreeMap<String, String>() {
            {
                put("user_id", MyApp.getInstance().getUser_id());
            }
        };
        NetHelper.get(APIServer.GET_SHOPCART, treeMap, true, new NetHelper.HttpCallBack() {
            @Override
            public void onSuccess(String s, Call call, Response response) {
                ShopCart bean = NetHelper.getBeanForJson(ShopCart.class, s);
                if (bean == null) return;
                if (bean.getCode().equals("200")) {
                    if (null != bean.getData()) {
                        if (bean.getData().size() != 0) {
                            if (ivRedPoint != null) {
                                ivRedPoint.setVisibility(View.VISIBLE);
                            }
                        } else {
                            if (ivRedPoint != null) {
                                ivRedPoint.setVisibility(View.GONE);
                            }
                        }
                    }
                }

            }
        });
    }

}
